package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IMokuaiDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.view.Mokuai;

public class MokuaiDaoImpl extends HibernateDaoSupport  implements IMokuaiDao {

	public void add(Mokuai mokuai) throws DataAccessException {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().save(mokuai);

	}

	public void delete(Integer mkId) throws DataAccessException {
		// TODO Auto-generated method stub
		
		Mokuai mokuai=(Mokuai) this.getHibernateTemplate().get(Mokuai.class, mkId);
		if(mokuai!=null){
			this.getHibernateTemplate().delete(mokuai);
		}

	}

	public Mokuai loadById(Integer mkId) throws DataAccessException {
		// TODO Auto-generated method stub
		Mokuai mokuai=(Mokuai) this.getHibernateTemplate().get(Mokuai.class, mkId);
		return mokuai;
	}

	public Mokuai loadByMingcheng(String mingcheng) throws DataAccessException {
		// TODO Auto-generated method stub
		Mokuai mokuai=null;
		String sql=" from Mokuai as m where 1=1 and m.mingcheng = ? ";
		List list=this.getHibernateTemplate().find(sql, mingcheng);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			mokuai=(Mokuai) list.get(0);
		}
		return mokuai;
	}

	public PageResultSet loadMokuaiByBean(Mokuai mokuai)
			throws DataAccessException {
		// TODO Auto-generated method stub
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from Mokuai as m where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(mokuai.getPage(), mokuai.getPageSize());
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        
        //模块名称
        if(mokuai.getMingcheng()!= null&&!mokuai.getMingcheng().equals("")){
            stringBuffer.append(" and m.mingcheng like ?");
            
            list.add("%"+mokuai.getMingcheng()+"%");
        }

        //删除
        if(mokuai.getShanchu()!= null){
        	stringBuffer.append(" and m.shanchu = ?");
        	
        	list.add(mokuai.getShanchu());
        }
		stringBuffer.append(" order by m.id desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);

		return pageResultSet;
	}

	public void update(Mokuai mokuai) throws DataAccessException {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().update(mokuai);

	}

}

















